<template>
  <div>
    <van-nav-bar fixed :title="baseInfo.Name" left-arrow class="color-white bg-red" @click-left="$utils.back($route.path)" />
    <div class="has-top pos-r has-bottom content" style="min-height:100vh;">
      <!-- 前三排行 -->
      <div class="honor-box pos-r" :style="{backgroundImage:'url('+require('@/assets/images/RewardHonorManage.png')+')'}">
        <div class="tab-box">
          <div class="tab">
            <span :class="activeTab===0?'tab-item tab-item-active':'tab-item'" @click="changeM(0)">{{ settleType===1?"今日":'本月' }}</span>
            <span :class="activeTab===1?'tab-item tab-item-active':'tab-item'" @click="changeM(1)">{{ settleType===1?"昨日":'上月' }}</span>
          </div>
        </div>
        <div class=" num-1 color-white">
          <headPortrait-box
            class="img-box pos-r br-full dib is-138"
            :head-portrait-url="firstData.Images"
          />
          <p class="fs-30 mt-35 text-show-line-1">{{ firstData.RealName }}</p>
          <p class="fs-36  text-show-line-1">{{ firstData.Performance }}</p>
        </div>
        <div class=" num-2 color-white">
          <headPortrait-box
            class="img-box pos-r br-full dib is-140"
            :head-portrait-url="secondData.Images"
          />
          <p class="fs-30 mt-25 text-show-line-1">{{ secondData.RealName }}</p>
          <p class="fs-36  text-show-line-1">{{ secondData.Performance }}</p>
        </div>
        <div class=" num-3 color-white">
          <headPortrait-box
            class="img-box pos-r br-full dib is-138"
            :head-portrait-url="thirdData.Images"
          />
          <p class="fs-30 mt-25 text-show-line-1">{{ thirdData.RealName }}</p>
          <p class="fs-36  text-show-line-1">{{ thirdData.Performance }}</p>
        </div>
      </div>
      <!-- 排行列表 -->
      <div class="mlr-25 br-12 ofh">
        <div class="flex-s-c bg-pink">
          <p class="color-red fs-26 lh-90">
            <svg-icon icon-class="jiangbei" class="fs-30 ml-20 mr-10" />
            <span>当前榜单：</span>
            <span class="fw-500">{{ baseInfo.Name }}</span>
          </p>
          <span class="arrow pos-r pr-43 flex-s-0" @click="showRule">规则说明</span>
        </div>
        <div class="bg-white">
          <ul v-show="list&&list.length>0" class="pt-10 pb-20">
            <li v-for="(item,index) in list" :key="index" class="li flex-v-c">
              <p class="ind">{{ item.RankNum }}</p>
              <headPortrait-box
                class="img-box pos-r br-full dib is-90 mtb-26"
                :head-portrait-url="item.Images"
              />
              <p class="ml-20 flex-1 fs-30 text-show-line-1">{{ item.RealName }}</p>
              <p class="mr-30 ml-20 fs-36 fw-b score tar">{{ item.Performance }}</p>
            </li>
          </ul>
          <p v-show="end===true&&list.length!==0" class="tac fs-36 ptb-20">无更多数据</p>
          <no-data v-show="list.length === 0" />
        </div>
      </div>
    </div>
    <div class="fixed-bottom  bottom-box bg-pink">
      <div class="pl-45 pr-55 flex-s-c full-height">
        <p class="fs-28 tle flex-s-0 flex-g-0">我的成绩</p>
        <headPortrait-box
          class="img-box pos-r br-full ml-20 dib is-90"
          :head-portrait-url="myData.Images"
        />
        <div class="ml-20 flex-1">
          <p class="fs-30 text-show-line-1 fw-500">{{ myData.RealName }}</p>
          <p v-if="myData.RankNum&&myData.RankNum>0" class="fs-26 fw-400">第<span class="color-red">{{ myData.RankNum }}</span>名</p>
          <p v-else class="fs-26 fw-400">您的业绩暂无排名</p>
        </div>
        <p class=" ml-20 fs-36 fw-b score tar">{{ myData.Performance }}</p>
      </div>
    </div>

    <!-- 规则弹窗 -->
    <RuleDialog :show-dialog="dialogShow" :list="descriptionList" @closed="dialogShow=false" />
  </div>
</template>

<script>
import headPortraitBox from 'components/headPortraitBox'
import RuleDialog from 'components/ruleDialog'
import noData from 'components/noData'
import { GetHonorRankPage, GetHonorRankSelf, GetRewardHonorSettingInfo } from 'api/RewardHonor'
export default {
  components: {
    headPortraitBox,
    RuleDialog,
    noData
  },
  data() {
    return {
      activeTab: 0,
      dialogShow: false,
      personData: '',
      thisMonth: '',
      lastMonth: '',
      today: '',
      yesterday: '',
      thisName: '',
      lastName: '',
      settleType: 0,

      // 数据
      list: [], // 排名列表
      firstData: {}, // 第一名信息
      secondData: {}, // 第二名信息
      thirdData: {}, // 第三名信息
      myData: {}, // 我的信息
      baseInfo: {}, // 基础信息
      descriptionList: [], // 说明

      // 请求参数
      settingId: '',
      period: '',
      requestData: {
        PageIndex: 1,
        PageSize: 10
      },

      // 滚动状态
      recordAmout: 0,
      totalAmout: 0,
      scollRequest: true,
      end: false
    }
  },
  watch: {
    activeTab(newVal, oldVal) {
      this.initRequistData()
      if (this.settleType === 2) {
        this.period = newVal === 0 ? this.thisMonth : this.lastMonth
        this.requestData.searchKey = newVal === 0 ? this.thisMonth : this.lastMonth
      } else if (this.settleType === 1) {
        this.period = newVal === 0 ? this.today : this.yesterday
        this.requestData.searchKey = newVal === 0 ? this.today : this.yesterday
      }
      this.GetHonorRankSelf()
      this.GetHonorRankPage()
    }
  },
  created() {
    this.initData()
  },
  mounted() {
    this.$nextTick(function() {
      window.addEventListener('scroll', this.onScroll)
    })
  },
  methods: {
    // 初始化数据
    initData() {
      this.settingId = this.$route.params.settingId
      this.thisMonth = this.$utils.dateFomat('yyyy-mm', new Date())
      this.lastMonth = this.$utils.lastMonth()
      this.today = this.$utils.dateFomat('yyyy-mm-dd', new Date())
      this.yesterday = this.$utils.dateFomat('yyyy-mm-dd', new Date(new Date().setDate(new Date().getDate() - 1)))
      this.GetRewardHonorSettingInfo()
    },
    // 重置请求参数
    initRequistData() {
      this.requestData.PageIndex = 1
      this.requestData.PageSize = 10
      this.list = []
      this.firstData = {}
      this.secondData = {}
      this.thirdData = {}
      this.totalAmout = 0
      this.scollRequest = true
      this.end = false
    },
    // 切换计算周期
    changeM(ind) {
      this.activeTab = ind
    },
    // 显示规则
    showRule() {
      this.dialogShow = true
    },
    GetHonorRankPage() {
      GetHonorRankPage({ settingId: this.settingId }, this.requestData).then(res => {
        if (res && res.Data && res.Data.length > 0) {
          res.Data.forEach(item => {
            switch (item.RankNum) {
              case 1:
                this.firstData = item
                break
              case 2:
                this.secondData = item
                break
              case 3:
                this.thirdData = item
                break
              default:
                this.list.push(item)
                break
            }
          })
          this.totalAmout = res.TotalAmout
          this.recordAmout = res.RecordAmout
          this.scollRequest = true
        }
      })
    },
    GetRewardHonorSettingInfo() {
      const _this = this
      GetRewardHonorSettingInfo({ settingId: this.settingId }).then(res => {
        if (res && res.Data) {
          _this.baseInfo = res.Data || {}
          // 商家介绍
          if (res.Data.Description) {
            if (res.Data.Description.indexOf('\n') >= 0) {
              const arr = res.Data.Description.split('\n')
              arr.forEach(function(item) {
                if (item) {
                  _this.descriptionList.push(item)
                }
              })
            } else {
              _this.descriptionList.push(res.Data.Description)
            }
          } else {
            _this.descriptionList = ['暂无说明']
          }
          _this.settleType = res.Data.SettleType
          if (_this.settleType === 2) {
            _this.requestData.searchKey = _this.thisMonth
            _this.period = _this.thisMonth
          }
          if (_this.settleType === 1) {
            _this.requestData.searchKey = _this.today
            _this.period = _this.today
          }
          _this.GetHonorRankSelf()
          _this.GetHonorRankPage()
        }
      })
    },
    GetHonorRankSelf() {
      GetHonorRankSelf({ settingId: this.settingId, period: this.period }).then(res => {
        if (res) {
          this.myData = res.Data || {}
        }
      })
    },

    // 滚动事件触发下拉加载
    onScroll() {
      const _this = this
      if (_this.scollRequest === true) {
        if (_this.$utils.getScrollHeight() - _this.$utils.getClientHeight() - _this.$utils.getScrollTop() <= 5) {
          _this.scollRequest = false
          if (_this.requestData.PageIndex < _this.totalAmout) {
            _this.requestData.PageIndex++
            _this.GetHonorRankPage()
          } else {
            _this.end = true
          }
          // }
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.bg-red{
  background: #ff2c2a;
}
.color-red{
  color: #ff2c2a;
}
.bg-pink{
  background:#FFF5F5;
}
.color-white{
  /deep/.van-nav-bar__title,/deep/.van-icon{
    color:$colorF;
  }
}
/deep/.van-hairline--bottom::after{
    border-bottom: none;
  }
.content{
  background: linear-gradient(to bottom, #ff2c2a 0%,#f7654c 100%);
  .honor-box{
    width: 750px;
    height: 582px;
    background-size: 100% ;
    background-repeat: no-repeat;
    .tab-box{
      // display: flex;
      text-align: center;
      margin: 70px  auto 0;
      .tab{
        display: inline-block;
        padding: 8px;
        border-radius:40px ;
        background: rgba(255,255,255,0.2);
        .tab-item{
          display: inline-block;
          font-size: 28px;
          line-height: 64px;
          padding: 0 76px;
          color: $colorF;
          font-weight: bold;
        }
        .tab-item-active{
          background: $colorF;
          color:#EA5549;
          border-radius: 32px;
        }
      }
    }
    .num-1,.num-2,.num-3{
      position: absolute;
      text-align: center;
      width: 270px;
      top: 217px;
      left:240px
    }
    .num-2{
      width: 190px;
      top: 279px;
      left:49px
    }
    .num-3{
      width: 190px;
      top: 300px;
      left: auto;
      right:49px
    }
  }
  .arrow:after{
    right: 20px;
    top: 10px;
  }
  .li{
    .ind{
      width:100px;
      text-align: center;
      color: $color9;
      font-size: 26px;
    }
  }
}

.bottom-box{
  height:120px;
  width:100%;
  overflow: hidden;
  .tle{
    width: 56px;
  }
}
.score{
  width: 170px;
}

</style>
